from typing import Optional
from flask import Blueprint, Response, request
from application.logic.ImageLogic import ImageLogic
from application.util.ResponseUtil import ResponseUtil
from application.middleware.LoginMiddleware import login_required

# 图片蓝图
image: Blueprint = Blueprint("image", __name__)


@image.post("/upload_image")
@login_required
def upload_image(token: str) -> Response:
    """
    上传图片
    :param token: Token
    """
    result: Optional[dict] = ImageLogic.upload_image(image_storage=request.files.get("image"))
    return ResponseUtil(data=result).success()


@image.post("/delete_image")
@login_required
def delete_image(token: str) -> Response:
    """
    删除图片
    :param token: Token
    """
    ImageLogic.delete_image_by_id(image_id=request.args.get("id"))
    return ResponseUtil().success()


@image.get("/get_images")
def get_image() -> Response:
    """
    根据ID列表（用逗号隔开），获取图片信息列表
    """
    results: list = ImageLogic.get_image_info_by_ids(image_ids=request.args.get("ids"))
    return ResponseUtil(data=results).success()
